﻿using System;
using System.Collections.Generic;
using PreeRegisterModel;
using Newtonsoft.Json.Linq;
using System.Data;

namespace PreeRegisterBLL
{
    public class SynBLL
    {
        #region 长治和平接口 接收绑定关系

        /// <summary>
        /// 长治和平 接收绑定关系
        /// </summary>
        /// <param name="notPayRequestModel"></param>
        /// <returns></returns>
        public static SynBindResponseModel SynBind(SynBindRequestModel synBindRequestModel)
        {
            SynBindResponseModel synBindResponseModel = new SynBindResponseModel();

            try
            {
                hars_healthuserbind bll_bind = new hars_healthuserbind();
                PreeRegisterModel.hars_healthuserbind model_bind = new PreeRegisterModel.hars_healthuserbind();
                model_bind = bll_bind.GetModel(synBindRequestModel.hospid, synBindRequestModel.pid, synBindRequestModel.userid);
                int bindsate = 0;
                int.TryParse(synBindRequestModel.opcode, out bindsate);
                if (model_bind != null)
                {
                    if (string.Equals(model_bind.bindstate, bindsate))
                    {
                        synBindResponseModel.code = "1";
                        synBindResponseModel.state = "0";
                        synBindResponseModel.msg = "接收绑定关系成功,绑定关系已存在。";
                        synBindResponseModel.result = "";
                    }
                    else
                    {
                        //更新绑定状态
                        if (bll_bind.Update(model_bind.id, bindsate))
                        {
                            synBindResponseModel.code = "1";
                            synBindResponseModel.state = "0";
                            synBindResponseModel.msg = "接收绑定关系成功,绑定状态更新成功。";
                            synBindResponseModel.result = "";
                        }
                        else
                        {
                            synBindResponseModel.code = "0";
                            synBindResponseModel.state = "0";
                            synBindResponseModel.msg = "接收绑定关系成功,绑定状态更新失败。";
                            synBindResponseModel.result = "";
                        }
                    }
                }
                else if (model_bind == null && bindsate == 1)
                {
                    model_bind = new PreeRegisterModel.hars_healthuserbind();
                    model_bind.bindstate = bindsate;
                    model_bind.hospid = synBindRequestModel.hospid;
                    model_bind.phone = synBindRequestModel.phone;
                    model_bind.pid = synBindRequestModel.pid;
                    model_bind.userid = synBindRequestModel.userid;
                    model_bind.createdate = model_bind.lastdate = DateTime.Now;

                    if (bll_bind.Add(model_bind))
                    {
                        synBindResponseModel.code = "1";
                        synBindResponseModel.state = "1";
                        synBindResponseModel.msg = "接收绑定关系成功。";
                        synBindResponseModel.result = "";
                    }
                    else
                    {
                        synBindResponseModel.code = "0";
                        synBindResponseModel.state = "0";
                        synBindResponseModel.msg = "接收绑定关系失败，请稍后重试。";
                        synBindResponseModel.result = "";
                    }
                }
                else
                {
                    synBindResponseModel.code = "1";
                    synBindResponseModel.state = "0";
                    synBindResponseModel.msg = "接收绑定关系成功，无效的绑定关系。";
                    synBindResponseModel.result = "";
                }
            }

            catch (Exception err)
            {
                synBindResponseModel.code = "0";
                synBindResponseModel.state = "0";
                synBindResponseModel.msg = "接收绑定关系失败，请稍后重试。";
                synBindResponseModel.result = err.ToString();
            }
            //返回
            return synBindResponseModel;
        }
        #endregion
    }
}
